/*
 * @Descripttion: 
 * @version: 
 * @Author: xcb
 * @Date: 2022-02-10 09:55:53
 * @LastEditors: xcb
 * @LastEditTime: 2022-02-24 14:47:15
 */
import {createRouter,createWebHistory} from "vue-router"
const routes = [
    {
        path:"/login",
        name:"login",
        component:() => import('@/components/Login.vue')
    },
    {
        path:"/",
        name:"Index",
        component:() => import('@/components/Olmap.vue')
    },
    {
        path:"/detail",
        name:"detail",
        component:() => import('@/components/Video.vue')
    }
]
const router = createRouter({
    history:createWebHistory(),
    routes
})

router.beforeEach((to, from, next) => {
    if(to.name == 'login') {
        next()
    } else {
        const token = window.localStorage.getItem('token')
        const exp = window.localStorage.getItem('exp')
        console.log(token,exp);
        if(!token || !exp) {
            window.localStorage.clear()
            router.push('/login')
        } else {
            let date = Date.parse(new Date()) / 1000
            let expDate = parseInt(exp);
            console.log(date,expDate,date < expDate);
            if(date < expDate){
               next()
            } else {
                window.localStorage.clear()
                router.push('/login')
            }
        }
    }
  })
export default router